package com.kuaimacode.kframework.api.controller.agent;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.api.controller.BaseController;
import com.kuaimacode.kframework.api.jwt.JwtTokenUtil;
import com.kuaimacode.kframework.api.service.SysAgentCategoryCfgService;
import com.kuaimacode.kframework.api.service.category.HotGoodsService;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.component.ComponentUtil;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgentCategoryCfg;
import com.kuaimacode.kframework.mybatis.models.category.HotGoods;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 分站物品品类设置 前端控制器
 * </p>
 *
 * @author Bean
 * @since 2019-03-12
 */
@RestController
@RequestMapping("/category")
@Api(tags = "物品种类",description = "获取分站物品品类及相关信息api")
public class CategoryRestController extends BaseController {
    @Value("${jwt.header}")
    private String tokenHeader;

    @Autowired
    private SysAgentCategoryCfgService sysAgentCategoryCfgService;

    @Autowired
    private HotGoodsService hotGoodsService;


    @Autowired
    private ComponentUtil componentUtil;


    @ApiOperation(value = "根据类型获取物品品类信息", notes="根据类型获取物品品类信息")
    @GetMapping("/listByType")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "物品品类类型1帮我送 2 帮我取 3 随意购", required = true, dataType = "string" ),
            @ApiImplicitParam(name = "agentId", value = "所在分站id，即需要查询的地区的行政区划代码", required = true, dataType = "string" ),
    })
    public JsonResult<SysAgentCategoryCfg> listByType(@RequestParam String type, @RequestParam String agentId,HttpServletRequest request){
        List<SysAgentCategoryCfg> list = sysAgentCategoryCfgService.getCategoryByType(agentId,type);

        Optional.ofNullable(list).ifPresent(categoryCfgs -> categoryCfgs.forEach(categoryCfg -> {
            categoryCfg.setIcon(componentUtil.convertImgAdminUrl(categoryCfg.getIcon()));
            categoryCfg.setIconPre(componentUtil.convertImgAdminUrl(categoryCfg.getIconPre()));
        }));
        return JsonResult.success("操作成功",list);
    }


    @ApiOperation(value = "根据物品品类查询随意购热门商品", notes="根据用户所在分站查找分站随意购热门商品，如果当前地区未开通返回系统默认商品")
    @GetMapping("/hotGoods/{categoryId}")
    @ApiImplicitParam(name = "categoryId", value = "物品品类id", required = true, dataType = "string" )
    public JsonResult<HotGoods> queryHotGoodsCategory(@PathVariable String categoryId, @RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
                                            @RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
                                            HttpServletRequest request){


        SysAgentCategoryCfg categoryCfg = sysAgentCategoryCfgService.getById(categoryId);
        String hotGoods = categoryCfg.getHotGoods();
        List<HotGoods> list = new ArrayList();
        if(hotGoods!=null){
            for(String goodsName:hotGoods.split(",")){
                HotGoods hotGoods1 = new HotGoods();
                hotGoods1.setGoodsName(goodsName);
                list.add(hotGoods1);
            }

        }

        return JsonResult.success(list);
    }











}
